package com.zy.shiro.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.shiro.domain.SysMenu;
import com.zy.shiro.domain.SysRole;
import com.zy.shiro.domain.SysRoleMenu;
import com.zy.shiro.mapper.SysMenuMapper;
import com.zy.shiro.mapper.SysRoleMapper;
import com.zy.shiro.mapper.SysRoleMenuMapper;
import com.zy.shiro.service.SysRoleService;
import com.zy.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper,SysRole> implements  SysRoleService{

  @Autowired
  private  SysRoleMapper sysRoleMapper;
  @Autowired
  private SysRoleMenuMapper sysRoleMenuMapper;
  @Autowired
  private SysMenuMapper sysMenuMapper;

  @Override
  public  IPage<SysRole> queryAllRole(Page<SysRole> page,RoleVo roleVo) {
//    Integer limit = roleVo.getLimit();
//    Integer page = roleVo.getPage();
//    IPage<SysRole> iPage=new IPage<SysRole>();
//    page.addOrder(OrderItem.desc("create_time"));
    IPage<SysRole> iPage=  sysRoleMapper.selectPage(page,new LambdaQueryWrapper<SysRole>()
      .like(StringUtils.hasText(roleVo.getRoleName()),SysRole::getRoleName,roleVo.getRoleName())
      .like(StringUtils.hasText(roleVo.getRemark()),SysRole::getRemark,roleVo.getRemark()));
    return iPage;
  }

  @Override
  public List<SysMenu> queryAllMenuByRoleId(Integer roleId) {
    List<SysMenu> menuList=null;
    // 2 通过角色的id 查询菜单的id
    List<Object> menuIds = sysRoleMenuMapper.selectObjs(new LambdaQueryWrapper<SysRoleMenu>().select(
      SysRoleMenu::getMenuId
      ).eq(SysRoleMenu::getRoleId, roleId)
    );
    if (menuIds == null || menuIds.isEmpty()) {
      return menuList;
    }
    // 3 通过菜单的Id 查询菜单的数据
    List<SysMenu> sysMenus = sysMenuMapper.selectList(new LambdaQueryWrapper<SysMenu>()
//      .eq(SysMenu::getMenuType,0)
//      .eq(SysMenu::getMenuType,1)
      .in(SysMenu::getMenuId, menuIds));
    return sysMenus;
  }

//  @Override
//  public IPage<SysRole> findByPage(Page<SysRole> page, SysRole sysRole) {
//    page.addOrder(OrderItem.desc("create_time"));
//    IPage<SysRole> sysRoleIPage = sysRoleMapper.selectPage(page, new LambdaQueryWrapper<SysRole>().
//      like(
//        StringUtils.hasText(sysRole.getRoleName()),
//        t -> {
//          return sysRole.getRoleName();
//        },
//        sysRole.getRoleName()
//      ));
//    return sysRoleIPage;
//  }

//  @Override
//  public List<SysRole> getAllRole() {
//    sysRoleMapper.selectCount()
//    return null;
//  }
}
